<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Router Class สำหรับค้นหา controller , method & vars จาก url ที่เรียกเข้ามา
 *
 * @author	Nawattapon Yoskamtorn
 * @link	http://www.devlism.com
 */
include BASEPATH . '/config/' . 'routes.php';

class DL_Router extends Devlism
{
    var $uri = '';
    var $base_path;
    var $config = array();
    var $route = array();
    var $suffix = '';
    var $controller = array();
    var $vars = array();
    var $segments = array();
    
    function __construct()
    {
	GLOBAL $route;
	GLOBAL $config;
	
	$this->base_path = substr($_SERVER["SCRIPT_FILENAME"] , 0 , -10);
	$this->route = $route;
	$this->config = $config;
	$this->suffix	=  $this->config['url_suffix'];
	
	if(isset($_SERVER[$this->config['uri_protocol']])){
	    $this->uri = trim($_SERVER[$this->config['uri_protocol']],'/');
	}else{
	    $this->uri = '';
	}
	
	$this->controller = $this->get_controller();
    }
    
    function __destruct()
    {
	
    }
    
    /**
    * get controller folder & file (method)
    * @access	private
    * @return	array controller (folder,file,[method])
    */
    private function get_controller()
    {
	$this->check_route();
	$this->set_segment();
	return $this->check_controller($this->segments);
    }
    
    /**
    * check uri from $route in config/route.php
    * @access	private
    * @return	array controller (folder,file,[method])
    */
    private function check_route() // ตรวจสอบ url กับ route ว่าตรงหรือไม่
    {
	// Turn the segment array into a URI string
	$uri = $this->uri;
	// Is there a literal match?  If so we're done
	if (isset($this->route[$uri])){
	    return $this->_set_route($this->route[$uri]);
	}
	
	// Loop through the route array looking for wild-cards
	foreach ($this->route as $key => $val){
	    // Convert wild-cards to RegEx
	    $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
	    // Does the RegEx match?
	    if (preg_match('#^'.$key.'$#', $uri)){
		// Do we have a back-reference?
		if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE){
		    $val = preg_replace('#^'.$key.'$#', $val, $uri);
		}
		
		return $this->_set_route($val);
	    }
	}
    }
    
    private function _set_route($uri)
    {
	$this->uri = urldecode($uri);
    }
    
    /**
    * ตรวจสอบ segment ว่ามีขึ้นต้นด้วย interger อยู่หรือเปล่า (ไม่มีชื่อ class หรือ method ที่ขึ้นต้นด้วยตัวเลข หรือ _ และต้องตามด้วย ตัวอกัษรภาษาอังกฤษ ตัวเลข กับ _ เท่านั้น)
    * @access	private
    * @return	array controller (folder,file,[method])
    */
    private function set_segment()
    {
	if($this->uri!=''){
	    $this->segments = explode('/',$this->uri);
	    foreach($this->segments as $index=>$val){
		if(!validate_class_name($val)){
		    $this->vars = array_slice($this->segments,$index);
		    $this->segments = array_slice($this->segments,0,$index);
		    break;
		}
	    }
	}
    }
    
    private function check_controller($segment) // อันนี้ ไม่ใช้ module ซ้อน module
    {
	//var_dump($segment);
	//var_dump($this->vars);
	$segment_temp = $segment;
	
	$count_segments = count($segment);
	if($count_segments===0){
	    $result['module'] = $this->route['default_controller'];
	    $result['folder'] = '';
	    $result['class'] = $this->route['default_controller'];
	    $result['method'] = 'index';
	    return $result;
	}
	
	// ตรวจตัวเองเป็น class
	$class = end($segment);
	$module = $segment['0'];
	array_pop($segment);
	array_shift($segment);
	$folder = implode('/' , $segment);
	$folder = trim($folder , '/');
	if($folder!==''){
	    $folder .= '/';
	}
	//echo "a => modules/$module ==> modules/$module/controllers/{$folder}{$class}_controller.php<br /><br />";
	if(file_exists("{$this->base_path}/modules/$module/controllers/{$folder}") && is_file("{$this->base_path}/modules/$module/controllers/{$folder}{$class}_controller.php")){
	    $result['module'] = $module;
	    $result['folder'] = $folder;
	    $result['class'] = $class;
	    $result['method'] = 'index';
	    return $result;
	}
	
	// ตรวจตัวเองเป็น method
	$method = end($segment_temp);
	$class = end($segment);
	if(!$class){
	    $class = $module;
	}
	array_pop($segment);
	$folder = implode('/' , $segment);
	$folder = trim($folder , '/');
	if($folder!==''){
	    $folder .= '/';
	}
	
	//echo "c ({$class}) => modules/{$module} ==> modules/{$module}/controllers/{$folder}{$class}_controller.php ({$method})<br /><br /><hr /><br /><br />";
	if(file_exists("{$this->base_path}/modules/{$module}/controllers/{$folder}") && is_file("{$this->base_path}/modules/{$module}/controllers/{$folder}{$class}_controller.php")){
	    include_once("{$this->base_path}/modules/{$module}/controllers/{$folder}{$class}_controller.php");
	    if(method_exists("{$class}_Controller", $method)){
		//array_shift($this->vars);
		$result['module'] = $module;
		$result['folder'] = $folder;
		$result['class'] = $class;
		$result['method'] = $method;
		return $result;
	    }
	}
	
	$segment_temp = $this->set_var($segment_temp);
	return $this->check_controller($segment_temp);
    }
    
    private function set_var($segment)
    {
	array_unshift($this->vars, end($segment));
	array_pop($segment);
	return $segment;
    }
}

/* End of file Routers.class.php */
/* Location: ./core/Routers.class.php */